package com.sywb.chuangyebao.contract;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;

import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.bean.Comment;
import com.sywb.chuangyebao.bean.NewsDetail;
import com.sywb.chuangyebao.utils.HttpCallback;
import com.sywb.chuangyebao.utils.HttpUtils;
import com.sywb.chuangyebao.utils.Utils;
import com.sywb.chuangyebao.view.NewsDetailActivity;
import com.sywb.chuangyebao.view.SearchActivity;
import com.sywb.chuangyebao.view.dialog.ShareDialog;
import com.sywb.chuangyebao.widget.NewsDetailHeaderView;

import org.bining.footstone.utils.ToastUtils;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

public interface NewsDetailContract {

    interface View extends SupportCommentDetailContract.View {
        int getArticleId();

        void setConsultState(boolean isConsult);
    }

    class Presenter extends SupportCommentDetailContract.Presenter<View> {

        protected NewsDetailHeaderView mHeaderView;

        private NewsDetail newsDetail;

        @Override
        public void onStart() {
            super.onStart();
            topicType = Constants.TOPIC_TYPE_ARTICLE;
            topicId = mView.getArticleId();

            initData();
        }

        public void changeCommentCount(int postion, int count) {
            if (postion < recyclerAdapter.getDataCount() && postion >= 0) {
                Comment comment = recyclerAdapter.getDatas().get(postion);
                comment.reply_count = count;
                recyclerAdapter.notifyDataSetChanged();
            }
        }

        public void changeCommentThumb(int postion, boolean isThumb, int thumbCount) {
            if (postion < recyclerAdapter.getDataCount()) {
                Comment comment = recyclerAdapter.getDatas().get(postion);
                comment.is_thumb = isThumb;
                comment.thumb_count = thumbCount;
                recyclerAdapter.notifyDataSetChanged();
            }
        }

        @Override
        public void initData() {
            super.initData();
            getNewsDetail();
        }

        @Override
        public void onStartLoadMore() {
            if (mHeaderView != null) {
                mHeaderView.setWebviewFocusable();
            }
            super.onStartLoadMore();
        }

        public void getNewsDetail() {
            HttpUtils.getArticleContent(topicId, new HttpCallback<NewsDetail>() {

                @Override
                public void onStart() {
                    super.onStart();
                    commonRecycler.setVisibility(android.view.View.INVISIBLE);
                }

                @Override
                public void onSuccess(NewsDetail data) {
                    newsDetail = data;

                    mHeaderView = new NewsDetailHeaderView(mActivity);
                    mHeaderView.setLayoutParams(new RecyclerView.LayoutParams(MATCH_PARENT, WRAP_CONTENT));
                    mHeaderView.setDetail(newsDetail, new NewsDetailHeaderView.NewsDetailHeaderViewListener() {

                        @Override
                        public void onLoadFinished() {
                            commonRecycler.setVisibility(android.view.View.VISIBLE);
                            loadDetailFinish();
                            onFinishAsync();
                        }

                        @Override
                        public void onUserClick() {
//                            mView.advance(UserIndexActivity.class, newsDetail.uid);
                            Utils.goUserIndex(mView, newsDetail.uid);
                        }

                        @Override
                        public void onFollowClick() {
                            follow(newsDetail.owner.uid, !newsDetail.owner.is_follow);
                        }

                        @Override
                        public void onProjectIndexClick() {
//                            mView.advance(UserIndexActivity.class, newsDetail.uid);
                            Utils.goUserIndex(mView, newsDetail.uid);
                        }

                        @Override
                        public void onProjectCallClick() {
                            goConsult();
                        }

                        @Override
                        public void onPraiseClick() {
                            agree(topicType, topicId, !newsDetail.is_thumb, -1);
                        }

                        @Override
                        public void onTagsClick(int postion) {
                            String key = newsDetail.tag.get(postion);
                            mView.advance(SearchActivity.class, key);
                        }

                        @Override
                        public void onRecommenItemClick(int articleId) {
                            mView.advance(NewsDetailActivity.class, articleId);
                        }
                    });
                    recyclerAdapter.setHeaderView(mHeaderView);

                    mView.changeCommentCount(newsDetail.count.comment);

                    mView.setIsFavorite(newsDetail.is_favorite);

                    if (newsDetail.owner != null && (newsDetail.owner.getUserRole() == Constants.USER_TYPE_PROJECT || newsDetail.owner.uid == 1234567 || newsDetail.owner.uid == 0)) {
                        mView.setConsultState(true);
                    } else {
                        mView.setConsultState(false);
                    }

                    onStartRefresh();
                }

                @Override
                public void onError(String strMsg) {
                    super.onError(strMsg);
                    onFinishAsync();
                    showMessage(strMsg);
                    if (mView != null) {
                        mView.exit();
                    }
                }
            });
        }

        @Override
        public void onRecyclerItemClick(int i) {
        }

        @Override
        void changeFollow() {
            ToastUtils.show(mContext, newsDetail.owner.is_follow ? "取消关注" : "关注成功");
            newsDetail.owner.is_follow = !newsDetail.owner.is_follow;
            mHeaderView.setIsFollow(newsDetail.owner.is_follow);
        }

        @Override
        void changeAgree() {
            newsDetail.is_thumb = !newsDetail.is_thumb;
            int count = 0;
            if (newsDetail.count != null) {
                if (newsDetail.is_thumb) {
                    newsDetail.count.thumb_up += 1;
                } else {
                    newsDetail.count.thumb_up -= 1;
                }
                count = newsDetail.count.thumb_up;
            } else {
                if (newsDetail.is_thumb) {
                    count = 1;
                } else {
                    count = 0;
                }
            }
            mHeaderView.setIsThumb(newsDetail.is_thumb, count);
        }

        @Override
        void changeFavorite() {
            ToastUtils.show(mContext, newsDetail.is_favorite ? "取消收藏" : "收藏成功");
            newsDetail.is_favorite = !newsDetail.is_favorite;
            mView.setIsFavorite(newsDetail.is_favorite);
        }

        @Override
        void changeComment(boolean isAdd) {
            if (isAdd) {
                newsDetail.count.comment += 1;
            } else {
                newsDetail.count.comment -= 1;
            }
            mView.changeCommentCount(newsDetail.count.comment);
        }

        public void goConsult() {
            super.goConsult(newsDetail.projectid, newsDetail.custid, Constants.COOKIE_PROJECT);
        }

        public void favorite() {
            favorite(!newsDetail.is_favorite);
        }

        public void showDialog() {
            if (newsDetail == null) {
                return;
            }
            ShareDialog sharedDialog = ShareDialog.newInstance(Constants.TOPIC_TYPE_ARTICLE, newsDetail.article_id, ShareDialog.TYPE_REPORT, newsDetail.title, !TextUtils.isEmpty(newsDetail.desc) && newsDetail.desc.length() > 40 ? newsDetail.desc.substring(0, 40) : newsDetail.desc, newsDetail.thumbnail);
            sharedDialog.show(mView.getMyFragmentManager(), "Shared");
        }

    }
}

